﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using ToDoIt2.Windows;
using System.Windows.Threading;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate;
using NHibernate;
using FluentNHibernate.Automapping;
using ToDoIt2.Model.Persistent;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using ToDoIt2.DB;
using ToDoIt2.UI;
using ToDoIt2.Model;
using ToDoIt2.Aspects;
//using PostSharp.Extensibility;
using NHibernate.Collection;
using System.Reflection;
using uNhAddIns.WPF.Collections.Types;
using uNhAddIns.WPF.Collections;
using System.ComponentModel;
using System.Windows.Controls;
using ToDoIt2.ViewModel;


// [assembly: AllClassReferences(AttributeTargetTypes = "ToDoIt2.*", AttributeTargetElements = MulticastTargets.Class)]

namespace ToDoIt2 {
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
	[NotifyPropertyChangedAttribute]
    public partial class App : Application {
		private Document _Document;
		public Document Document {
			get { return this._Document; }
			set { this._Document = value; this.DocumentVM = new DocumentVM(this._Document); }
		}

		public DocumentVM DocumentVM { get; set; }

		private FluentConfiguration _FluentConfig;
		public FluentConfiguration FluentConfig {
			get { return _FluentConfig; }
			private set { this._FluentConfig = value; }
		}

		private void OnStartup(object sender, StartupEventArgs e) {
			this.FluentConfig = Fluently.Configure()
									.ExposeConfiguration(cfg => cfg.SetProperty(NHibernate.Cfg.Environment.CollectionTypeFactoryClass,
															 typeof(WpfCollectionTypeFactory).AssemblyQualifiedName))
									.Mappings(m => m.AutoMappings
										.Add(AutoMap
												.Assemblies(new ToDoIt2.DB.AutoMapping(),
														Assembly.GetExecutingAssembly()
														)
												.IgnoreBase<pklib.DataObject>()
												.IgnoreBase<pklib.Model.ModelBase>()
												.Conventions.Add<myPrimaryKeyConvention>()
												.Conventions.Add<myForeignKeyConvention>()
										)
									);
			this.Document = new Document();
//			FrameworkElement.FocusVisualStyleProperty.OverrideMetadata(typeof(UIElement), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.OverridesInheritanceBehavior));
		}

		public void DisplayException(pklib.NoArgsDelegate z) {
			try { z(); } catch (Exception e) {
				new ExceptionMessage(e.ToString());
				this.Shutdown();
			}
		}

		private void OnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) {
			new ExceptionMessage(e.Exception.ToString());
			this.Shutdown();
		}
	}
}
